
#include "glmaterial.h"
#include <iostream>

namespace rendersystem {

    //-------------------------------------------
    // Classe Material
    //-------------------------------------------

    MyGLMaterial::MyGLMaterial(const loaders::Material &mat) : loaders::Material(mat){
        mKd=glm::vec3(mat.Kd[0],mat.Kd[1],mat.Kd[2]);
        mKs=glm::vec3(mat.Ks[0],mat.Ks[1],mat.Ks[2]);
        mNs=mat.shininess;

        hasDiffuse=0;
        hasSpecular=0;
        hasShininess=0;
        hasMap=0;

        Texture *defaultTex = new Texture("../data/default.jpg",GL_TEXTURE_2D);
        if(!map_kd.empty()){
            //std::cerr<<"init texture with : "<<map_kd<<std::endl;
            Texture *attribTex = new Texture("../data/"+map_kd,GL_TEXTURE_2D);
            attribTex->useMipMap(GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR);
            setTexture(attribTex);
        //    setspecularTexture(defaultTex);
         //   setshininessTexture(defaultTex);
         //   setmapTexture(defaultTex);
        }else{

            defaultTex->useMipMap(GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR);
            setTexture(defaultTex);
         //   setspecularTexture(defaultTex);
          //  setshininessTexture(defaultTex);
         //   setmapTexture(defaultTex);
        }
    }

    void MyGLMaterial::setTexture(Texture *tex){
        texture = tex;
        setdiffuseTexture(tex);


    }


    void MyGLMaterial::setdiffuseTexture(Texture *tex){
        diffusetexture = tex;
         hasDiffuse= 1.f;

    }
    void MyGLMaterial::setspecularTexture(Texture *tex){
        speculartexture = tex;
        hasSpecular=1.f;
    }
    void MyGLMaterial::setshininessTexture(Texture *tex){
        shininesstexture = tex;
        hasShininess=1.f;
    }
    void MyGLMaterial::setmapTexture(Texture *tex){
        maptexture = tex;
        hasMap=1.f;
    }

    void MyGLMaterial::bindGL(int parameterLocation[9]){
        glAssert( glUniform3fv( parameterLocation[0], 1, glm::value_ptr(mKd)) );
        glAssert( glUniform3fv( parameterLocation[1], 1, glm::value_ptr(mKs)) );
        glAssert( glUniform1f( parameterLocation[2], mNs) );

        if(hasDiffuse == 1.0f){
            diffusetexture->bind(0);
            glAssert( glUniform1i(parameterLocation[3], 0) );
        }
        if(hasSpecular == 1.0f){
            speculartexture->bind(1);
            glAssert( glUniform1i(parameterLocation[4], 1) );
        }
        if(hasShininess == 1.0f){
            shininesstexture->bind(2);
            glAssert( glUniform1i(parameterLocation[5],2) );
        }
        if(hasMap == 1.0f){
            maptexture->bind(3);
            glAssert( glUniform1i(parameterLocation[6], 3) );
        }


        glm::vec4  hasMultiText;
        hasMultiText.x=hasDiffuse;
        hasMultiText.y=hasSpecular;
        hasMultiText.z=hasShininess;
        hasMultiText.w=hasMap;

        glAssert( glUniform4fv( parameterLocation[7], 1, glm::value_ptr(hasMultiText)) );

        /*Texture *test = new Texture("blanc",GL_TEXTURE_2D);
        test->bind(3);
               glAssert( glUniform1i(parameterLocation[4], 3) );

*/

    }




}
